/**
 * Created by yanghaijin on 2018/5/12.
 */

let MsgCode = require('./../../../define/MsgCode');
let MAP_ID = require('./../../../define/MAP_ID');

let Room = require('./../logic/Room');

module.exports = function (app) {
    return new Handler(app);
};

let Handler = function (app) {
    this.app = app;
};

/**
 * 游戏消息处理
 * @param {Object} msg
 * @param {Object} session
 * @param {Function} next
 * */
Handler.prototype.onMessage = function (msg, session, next) {
    var self = this;
    let code = msg['code'];
    let room_id = msg['data']['room_id'];
    let room = this.app.id2Room[room_id];
    if (!room) {
        let param = {};
        param['code'] = code;
        param['errorCode'] = -1;
        param['msg'] = '房间号错误，退出房间失败！';
        next(null, param);
    }

    console.log(this.app.id2Room);
    switch (code) {
        case MsgCode.StartGame:
            self.app.id2Room[room_id].startGame(next);
            break;
    }
};

/**
 * 游戏通知处理
 * @param {Object} msg
 * @param {Object} sid
 * @param {Function} next
 * */
Handler.prototype.onNotify = function (msg, sid, next) {
    console.log('arguments', arguments);
    var self = this;
    let code = msg['code'];
    let room_id = msg['data']['room_id'];
    let room = this.app.id2Room[room_id];
    if (!room) {
        let param = {};
        param['code'] = code;
        param['errorCode'] = -1;
        param['msg'] = '房间号错误，退出房间失败！';
    }

    // 这里交给房间自己处理
    room.onNotify(msg, sid);
    next();
};